package com.zw.stock_sell_storage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.stock_sell_storage.constant.MessageCode;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.UserDto;
import com.zw.stock_sell_storage.dto.ex.UserDtoEx;
import com.zw.stock_sell_storage.service.IUserService;
import com.zw.stock_sell_storage.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping("/login")
    public JsonResult<UserDtoEx> login(@RequestBody UserDto userDto, HttpServletRequest request){
        JsonResult<UserDtoEx> jsonResult = new JsonResult();
        try {
            UserDtoEx userDtoEx = userService.queryUser(userDto);
            if (userDtoEx!=null){
                request.getSession().setAttribute("user",userDtoEx);
            }
            else{
                jsonResult.setMessage(MessageCode.ACCOUNT_OR_PASSWORD_ERROR.message);
                jsonResult.setCode(MessageCode.ACCOUNT_OR_PASSWORD_ERROR.code);
            }
            jsonResult.setData(userDtoEx);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMessage(MessageCode.ERROR.message);
            jsonResult.setCode(MessageCode.ERROR.code);
        }
        return jsonResult;
    }

    @RequestMapping("/queryUserList")
    public Page<UserDto> queryUserList(@RequestBody MyPageDto<UserDto> myPageDto){
        return userService.queryUserList(myPageDto);
    }

    @RequestMapping("/addUser")
    public boolean addUser(@RequestBody UserDtoEx userDtoEx){
        return userService.addUser(userDtoEx);
    }

    @RequestMapping("/rmvUser")
    public boolean rmvUser(int userId){
        return userService.rmvUser(userId);
    }

    @RequestMapping("/queryUserById")
    public UserDtoEx queryUserById(int userId){
        return userService.queryUserById(userId);
    }

    @RequestMapping("/updateUser")
    public boolean updateUser(@RequestBody UserDtoEx userDtoEx){
        return userService.updateUser(userDtoEx);
    }

    @RequestMapping("/changePassWord")
    public boolean changePassWord(@RequestBody UserDto userDto){
        return userService.changePassWord(userDto);
    }

}
